// condition_variable.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <thread>
#include <mutex>
#include <string>
#include "NetMgr.h"
#include "AfSocket.h"
bool fun()
{
    printf("#############$$$$$$$$$$A \n");

    return false;
}

class testUdpSocket
{
public:
    testUdpSocket()
    {

		int ret = m_oReceiver.Open(AfSockAddr("127.0.0.1", 50058));
        channel = std::make_shared<Channel>(m_oReceiver.hSock);
        
        channel->SetReadCallback([this]() {
            
            printf("read ... \n");
            char buf[1500];
            int len = 1500;
            AfSockAddr mAfSockAddr;
            m_oReceiver.RecvFrom(buf, len, mAfSockAddr);
            });

        channel->EnableReading();
        

        ret = m_oSender.Open(AfSockAddr("127.0.0.1", 50059));
    }
    void run()
    {
        NetMgr::GetInstance()->Init(1);
        std::shared_ptr<WorkerThreadN> p = NetMgr::GetInstance()->m_oEventLoop.GetTaskScheduler();
        p->UpdateChannel(channel);

		for (int i = 0; i < 1000; i++)
		{
			send();
		}


		getchar();
    }

    void send()
    {
        
        
        char buf[1500] = {1};
        m_oSender.SendTo(buf, 1500, AfSockAddr("127.0.0.1", 50058));

        printf("####### send \n");
    }

    ChannelPtr channel;
    AfUdpSocket m_oReceiver;
    AfUdpSocket m_oSender;
};


static int count = 0;

int main()
{
    printf("hello world \n");
   


#if 1
    testUdpSocket t_testUdpSocket;
    t_testUdpSocket.run();
#endif


    //testSocket();
    //testNetMgr();
    //EventLoop tEventLoop;

    //tEventLoop.AddTimer(1000, fun);

    //getchar();



    //Event tEvern;
    //tWorkerThread.PostTask([&tEvern]() {
    //    printf("$$$$$$$$$$$$ \n");
    //    
    //    tEvern.Set(0);
    //    });


    //tEvern.Wait();
    //printf("over \n");


    int tCount = 0;



    char buffer[10000];

    void* p = (void *)buffer;

    printf("p:%p \n", p);


}
